package collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**泛型的演示*/
public class GenericDemo {
    public static void main(String[] args) {

        Collection<Point> x = new ArrayList<>();
        x.add(new Point(1,2));//自动重写了toString
        x.add(new Point(3,4));
        x.add(new Point(5,6));
        x.add(new    Point(7,8));

        Iterator<Point> it = x.iterator();
        while(it.hasNext()){
            Point p = new Point();
            System.out.println(p);
        }

        for(Point p:x){
            System.out.println(p);
        }


        Collection<Integer> str = new ArrayList<>();
        str.add(1);
        str.add(2);
        str.add(3);
        str.add(4);
        str.add(5);
//        str.add("e");//编译错误，e的类型违背了集合c所指向的泛型的实际类型String

        //迭代器所指定的泛型应当与其遍历的集合的泛型一致
        Iterator<Integer> it1 = str.iterator();
        while(it1.hasNext()){
            Integer baby = it1.next();
            System.out.println(baby);
        }

        for (Integer baby:str){
            System.out.println(baby);
        }

    }
}
